<?php
namespace app\index\controller;
use think\Controller;
use app\index\domain as domain;

class Index extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if(IS_POST)
            if(I('?get.form'))
                if(A('Form')->add())
                    $this->assign('formOK', true);
                else
                    $this->assign('formOK', false);
        $site = domain\Classes::get('Option');
        $links = domain\Classes::get('Links');
        $termList = domain\Classes::get('TermListById', 0);
        $this->view->assign('termList',$termList);
        $this->view->assign('site',$site);
        $this->view->assign('links',$links);
    }

    /**
     * 显示首页
     */
    public function index()
    {
        return $this->fetch('index/index');
    }

    /**
     * 显示栏目页
     */
    public function term($termId=null)
    {
        if(is_null($termId)){
            $flag = I('get.flag');
            $termId   = D('Term')->getIdByFlag($flag);
        }
        $term = domain\Classes::get('TermById', $termId);
        $tpl = D('TermModel')->getTpl($term['model_id'], 'term');
        if(empty($tpl))
            return $this->index();
        if($tpl == 'jump')
            return $this->term($term['child']['0']['id']);
        if($tpl == 'jump-content')
            return $this->content($term['content']['0']['id']);
        $this->assign('term', $term);
        trace('tpl='.$tpl.', id='.$termId,'log');
        return $this->fetch('index/'.$tpl);
    }

    /**
     * 显示内容页
     */
    public function content($contentId=null)
    {
        if(is_null($contentId)){
            $contentId = I('get.id');
        }
        $content = domain\Classes::get('ContentById', $contentId);
        $term    = $content['term'];

        $tpl = D('TermModel')->getTpl($term['model_id'], 'content');
        if(empty($tpl))
            return $this->index();
        $this->assign('term', $term);
        $this->assign('content', $content);
        trace('tpl='.$tpl,'log');
        return $this->fetch('index/'.$tpl);
    }

    /**
     * 显示搜索页
     */
    public function search()
    {
        $modelId = I('?post.mid')? I('post.mid'): 0;
        $search = domain\Classes::get('Search', $modelId);
        $this->assign('search', $search);
        return $this->fetch('index/search');
    }
	
	/**
     * 首页留言
     */
	
	public function index_msg()
	{
		if(IS_POST){
			$data['name'] = I('post.name');//姓名
			$data['title'] = I('post.title');//单位名称
			$data['phone'] = I('post.phone'); //联系电话
			$data['email'] = I('post.email');//电子邮箱
			$data['message_type'] = I('post.message_type');//留言类型
			$data['create_time'] = time();//留言时间
			$data['ip'] = get_client_ip();//留言IP
			
			$msg = M('message')->add($data);
			 if($msg){
				 echo "<script>alert('提交成功！');history.back()</script>";
			 }else{
				echo "<script>alert('提交失败！');history.back()</script>";
			 }
		
		}
	}
	
	
	/**
     * 给我们留言
     */
	
		public function giveus_msg()
	{
		
		if(IS_POST){
			$data['name'] = I('post.name');//姓名
			$data['title'] = I('post.title');//公司名称
			$data['phone'] = I('post.phone'); //联系电话
			$data['email'] = I('post.email');//电子邮箱
			$data['content'] = I('post.content');//留言内容
			$data['message_type'] = I('post.message_type');//留言类型
			$data['ziliao'] = $_FILES['ziliao']['name'];//资料名称
			$totext=iconv("UTF-8","GBK",$_FILES['ziliao']['name']);
			$file_postion = move_uploaded_file($_FILES["ziliao"]["tmp_name"],"public/Uploads/".$totext);
			$data['create_time'] = time();//留言时间
			$data['ip'] = get_client_ip();//留言IP
			if($_FILES["ziliao"]["size"] != 0){
				if($_FILES["ziliao"]["size"] < 2090000){
					$msg = M('message')->add($data);	
					 if($msg){
						 echo "<script>alert('提交成功！');history.back()</script>";
					 }else{
						echo "<script>alert('提交失败！');history.back()</script>";
					 }
				}else{
					echo "<script>alert('上传文件限制在2M以内！');history.back()</script>";
				}
			}else{
				echo "<script>alert('上传文件限制在2M以内！');history.back()</script>";
			}
		}
		
	}
	
	/**
     * 成果需求
     */
	
		public function chengguo_msg()
	{
		if(IS_POST){
			$data['name'] = I('post.name');//姓名
			$data['title'] = I('post.title');//公司名称
			$data['phone'] = I('post.phone'); //联系电话
			$data['email'] = I('post.email');//电子邮箱
			
			$data['xqlb'] = I('post.xqlb');//需求类别
			$data['xqname'] = I('post.xqname');//需求名称
			$data['dwjj'] = I('post.dwjj');//单位简介
			$data['xqjj'] = I('post.xqjj');//需求简介
			
			$data['jctj'] = I('post.jctj');//基础条件
			$data['hzfs'] = I('post.hzfs');//合作方式
			
			$data['message_type'] = I('post.message_type');//留言类型
			$data['create_time'] = time();//留言时间
			$data['ip'] = get_client_ip();//留言IP
			$msg = M('message')->add($data);
			 if($msg){
				 echo "<script>alert('提交成功！');history.back()</script>";
			 }else{
				echo "<script>alert('提交失败！');history.back()'</script>";
			 }
		
		}
	}

    /**
     * 显示留言页
     */
    public function message()
    {
        $id = I('get.id');
        $termId = I('get.term_id');
        $term = domain\Classes::get('TermById', $termId);
        $tpl = D('TermModel')->getTpl($term['model_id'], 'content');
        $this->assign('term', $term);
        $this->assign('id', $id);
        return $this->fetch('index/'.$tpl);
    }
	
}

